package com.scalar.chiptrack.adaptor.beceem;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.FabSortLot;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.utils.Parser;
import com.scalar.chiptrack.adaptor.utils.ParserFactory;
import com.scalar.chiptrack.adaptor.utils.XLSParserImpl;
import com.scalar.chiptrack.adaptor.utils.ParserResult;
import com.scalar.chiptrack.adaptor.ikanos.utac.UTACPOP3Adaptor;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.FabSortAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Date;
import java.io.File;


/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 14 Apr, 2010
 * Time: 1:26:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class UTACDataParser extends JobHandler implements AdaptorConstants
{
    private static Logger m_parserLogger    = null;
    private String m_dataOutputDir          = null;

    private HashMap<String, AssemblyLot> m_assyWipDataMap    = new HashMap<String, AssemblyLot>();
    private HashMap<String, TestLot> m_ftDataMap             = new HashMap<String, TestLot>();
    private HashMap<String, InventoryLot> m_dieBankDataMap   = new HashMap<String, InventoryLot>();
    private HashMap<String, InventoryLot> m_waferBankDataMap = new HashMap<String, InventoryLot>();
    private HashMap<String, FabSortLot> m_sortDataMap        = new HashMap<String, FabSortLot>();
    private HashMap<String, ShippedLot> m_sortShipDataMap    = new HashMap<String, ShippedLot>();
    private HashMap<String, InventoryLot> m_fgDataMap        = new HashMap<String, InventoryLot>();
    private HashMap<String, ShippedLot> m_fgShipDataMap      = new HashMap<String, ShippedLot>();


    public UTACDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_UTAC, WIP_TYPE_ASSEMBLY );
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_UTAC );
            AssemblyAdaptorDAO.setLogger( m_parserLogger );   // All the DAOs and Parser should be logging into same file
            TestAdaptorDAO.setLogger( m_parserLogger);
            InventoryLotDAO.setLogger( m_parserLogger );
            FabSortAdaptorDAO.setLogger( m_parserLogger );
            ShippedLotDAO.setLogger( m_parserLogger );
            AdaptorUtils.setLogger( m_parserLogger );
        }
        catch (ScalarException se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading/creating UTAC output directory from database, Exception: " + se);
            throw se;
        }
    }
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main( String args[] ) throws Exception
    {
        try
        {
            new UTACDataParser().execute();
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    //Execute Method
   public void execute() throws Exception
   {
       AdaptorUtils.printHeader(VENDOR_NAME_UTAC, CONSTANT_PARSER );

       File doneDestDir = new File( m_dataOutputDir + "/done" );

       File wipDataoutDir = new File( m_dataOutputDir );

       File[] fileList = wipDataoutDir.listFiles();

       if ( ( fileList == null ) || ( fileList.length <= 3 ) )
       {
           log( "No file found in directory: " + wipDataoutDir );
           throw new ScalarException( "No file found in directory: " + wipDataoutDir + "----\n\n" );
       }

       ArrayList<File> successFileList   = new ArrayList<File>();
       boolean success             = false;

       try
       {
           for ( int i = 0; i < fileList.length; i++ )
           {
               if ( fileList[i].isDirectory() == true )
               {
                   continue;
               }
               
               if ( fileList[i].getName() != null && fileList[i].getName().trim().indexOf( UTACFTPAdaptor.FILE_NAME_WIP ) != -1  )
               {
                    parseDailyWipReport( fileList[i].getAbsolutePath() );

                    successFileList.add( fileList[i] );
               }
               /*if ( ( fileList[i].getName() != null ) && ( fileList[i].getName().trim().startsWith( UTACPOP3Adaptor.FILE_NAME_DAILY_SHIP ) || fileList[i].getName().trim().indexOf( UTACPOP3Adaptor.FILE_NAME_DAILY_SHIP ) != -1 ) )
               {
                    ArrayList utacShipList = parseDailyShipReport( fileList[i].getAbsolutePath() );

                   if ( utacShipList != null )
                   {
                       getShippingDetails( utacShipList );
                   }
                   successFileList.add( fileList[i] );
               }*/

               /*if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( UTACPOP3Adaptor.FILE_NAME_DAILY_YIELD ) )
               {
                    ArrayList testBinList = parseFTYieldReport( fileList[i].getAbsolutePath() );

                   if ( testBinList != null && testBinList.size() > 0 )
                    {
                        //to get the UTAC Test Bin Details
                        if ( testBinList.isEmpty() == false )
                        {
                            log( "Updating UTAC Binning Information" );
                            TestAdaptorDAO.saveTestBinInfo ( null, testBinList , VENDOR_NAME_UTAC );
                            success = true;
                        }
                        else
                        {
                            log( "UTAC Binning Information List is Empty" );
                        }
                    }
                   successFileList.add( fileList[i] );
               }*/
           }
       }
       catch ( Exception e )
       {
           success = false;
           log( "ERROR!! ERROR!! ERROR!! While parsing and storing File " + e );
           e.printStackTrace();
           throw e;
       }

       // to save the Wafer Store lot tables
       try
       {
           if ( m_waferBankDataMap.isEmpty() == false )
           {
               log(" Updating Wafer Store Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_waferBankDataMap.values().iterator(), LOCATION_TYPE_WAFER_BANK, VENDOR_NAME_UTAC );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_WAFER_BANK, VENDOR_NAME_UTAC  );
           }
       }
       catch (ScalarException se)
       {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Wafer Store Lot information into database: " + se);
          throw se;
       }

        // to save the SORT lot tables
       try
       {
           if ( m_sortDataMap.isEmpty() == false )
           {
               log( " Updating Sort Lot Information" );
               FabSortAdaptorDAO.saveSortLot( null, m_sortDataMap.values().iterator(), WIP_TYPE_SORT, VENDOR_NAME_UTAC );
               success = true;
           }
           else
           {
               FabSortAdaptorDAO.closeFabSortLot( null, WIP_TYPE_SORT, VENDOR_NAME_UTAC );
           }
       }
       catch (ScalarException se)
       {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Sort Lot information into database: " + se);
          throw se;
       }
       try
       {
            // Save Sort Information
            if ( m_sortShipDataMap.isEmpty() == false )
            {
                log( " Updating Sort Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_sortShipDataMap.values().iterator() );
            }
            success = true;
            m_sortShipDataMap.clear();
            m_sortShipDataMap = null;
       }
       catch (ScalarException se)
       {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Sort Shipping Lot information into database: " + se );
            throw se;
       }
       // to save the Die Bank lot tables
       try
       {
           if ( m_dieBankDataMap.isEmpty() == false )
           {
               log(" Updating Die Bank Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_dieBankDataMap.values().iterator(), INVENTORY_TYPE_DIE_BANK, VENDOR_NAME_UTAC );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, INVENTORY_TYPE_DIE_BANK, VENDOR_NAME_UTAC  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
          throw se;
      }

       // to save the Assembly lot tables
      try
      {
           if ( m_assyWipDataMap.isEmpty() == false )
           {
               log(" Updating Assembly Lot Information");
               AssemblyAdaptorDAO.saveAssemblyLot( null, m_assyWipDataMap.values().iterator(), WIP_TYPE_ASSEMBLY, VENDOR_NAME_UTAC );
               success = true;
           }
           else
           {
               AssemblyAdaptorDAO.closeAssemblyLot(null, WIP_TYPE_ASSEMBLY, VENDOR_NAME_UTAC );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
          throw se;
      }

      /*Updating the Test Lot Information*/
      try
      {
          // Save Test Information
          if( m_ftDataMap.isEmpty() == false )
          {
              log(" Updating Test Lot Information");
              TestAdaptorDAO.saveTestLot( null, m_ftDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_UTAC );
              success = true;
          }
          else
          {
              TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_UTAC );
          }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se);
          throw se;
      }
      // to save the FG lot tables
      try
       {
           if ( m_fgDataMap.isEmpty() == false )
           {
               log(" Updating Finshed Goods Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_fgDataMap.values().iterator(), LOCATION_TYPE_FG, VENDOR_NAME_UTAC );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_FG, VENDOR_NAME_UTAC  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Finshed Goods Lot information into database: " + se);
          throw se;
      }

      try
      {
          // Save FG Ship Information
          if ( m_fgShipDataMap.isEmpty() == false )
          {
              log( " Updating FG Shiping Lot Information" );
              ShippedLotDAO.saveShippedLot( null, m_fgShipDataMap.values().iterator() );
          }
          success = true;
          m_fgShipDataMap.clear();
          m_fgShipDataMap = null;
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log( "ERROR!! ERROR!! ERROR!! Could not update FG Shipping Lot information into database: " + se );
          throw se;
      }

       // To Close the Drop Out Lots
      AdaptorDAO.closeDropOutLots( VENDOR_NAME_UTAC );

      // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
      AdaptorDAO.sendAllNotifications( VENDOR_NAME_UTAC, true, true, true );

      int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( UTACPOP3Adaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

      // Update Adaptor_Run_Info table
      try
      {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UTACPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
      }
      catch ( ScalarException se )
      {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UTACPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
       }
        // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
   }//end of execute method


    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private void parseDailyWipReport(String fileName) throws Exception
    {
        if (fileName == null) throw new ScalarException("Filename argument is null");

        ParserResult result     = null;
        InventoryLot inventoryLot = null;
        FabSortLot   fabSortLot   = null;
        AssemblyLot  assemblyLot  = null;
        TestLot      testLot      = null;     

        try
        {
            File detailWipFile = new File(fileName);
            if (detailWipFile.exists() == false)
            {
                throw new ScalarException(fileName + " doesnt exist");
            }

            log("\nFound " + fileName);
            log("Parsing the file for UTAC WIP Status Report...");
            System.out.println("\nFound " + fileName);
            System.out.println("Parsing the file for UTAC WIP Status Report...");

            Parser parser = ParserFactory.getXLSParser( detailWipFile );
            
            ArrayList sheetNamesList = ((XLSParserImpl)parser).getSheetNamesList();

            for(int i= 0; i< sheetNamesList.size(); i++ )
            {
                String partNumber       = null;
                String fabLotNumber     = null;
                String lotNumber        = null;
                String lotStatus        = null;

                String sheetName = (String)sheetNamesList.get( i );
                if (sheetName != null && sheetName.indexOf("Wafer") != -1)
                {
                    log("sheetName = " + sheetName );
                    ((XLSParserImpl)parser).setHeaderRowIdx(2);
                    ((XLSParserImpl)parser).setDataStartRowIdx(3);

                    result = ((XLSParserImpl)parser).parse( sheetName );
                    //System.out.println("result.getHeadings() = " + result.getHeadings());
                    while ( result.next() )
                    {    
                        partNumber = result.getString("Cust Device");
                        fabLotNumber = result.getString("Cust LotID");
                        lotNumber  = result.getString("Cust LotID");

                        if( result.getString("Cur_State") != null && ( "WAIT".equalsIgnoreCase( result.getString("Cur_State") )
                                || "HELD".equalsIgnoreCase(result.getString("Cur_State") ) || result.getString("Cur_State").indexOf("HLD") != -1 ) )
                        {
                            lotStatus = LOT_STATUS_HOLD;
                        }
                        else
                        {
                            lotStatus = LOT_STATUS_RUNNING;
                        }

                        if ( lotNumber != null && lotNumber.trim().length() > 0 && partNumber != null && partNumber.trim().length() > 0)
                        {
                            //Wafer Store  LOTS
                            if ( result.getString("Pro_Area") != null && "WAFER".equalsIgnoreCase( result.getString("Pro_Area") ) )
                            {
                                log("In Wafer Store " + lotNumber );
                                inventoryLot = (InventoryLot) m_waferBankDataMap.get( partNumber + fabLotNumber );
                                if ( inventoryLot == null )
                                {
                                    inventoryLot = new InventoryLot(VENDOR_NAME_UTAC, LOCATION_TYPE_WAFER_STORE );
                                    m_waferBankDataMap.put( partNumber + fabLotNumber, inventoryLot );
                                }
                                int quantity = 0;
                                if (inventoryLot.getWaferQtyIn() != null)
                                {
                                    try
                                    {
                                        quantity = Integer.parseInt(inventoryLot.getWaferQtyIn());
                                    }
                                    catch (NumberFormatException ignore) {
                                    }
                                    quantity += result.getInt("Cur_Qty");
                                }
                                else
                                {
                                    quantity = result.getInt("Cur_Qty");
                                }

                                if ( quantity > 0 )
                                {
                                    inventoryLot.setLotNumber( fabLotNumber );
                                    inventoryLot.setVendorLot( lotNumber );
                                    inventoryLot.setPartNumber( partNumber );
                                    inventoryLot.setStatus( lotStatus );
                                    inventoryLot.setFabLotNumber( fabLotNumber );
                                    inventoryLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                                    inventoryLot.setWaferQtyIn( quantity +"" );
                                    inventoryLot.setStage( result.getString("Cur_Stage"));
                                    inventoryLot.setCustomerPO( result.getString("PO_NUM") );
                                    if ( result.getDate("Rec_Date") != null )
                                    {
                                        inventoryLot.setDateIn( StringUtils.formatDateToString(result.getDate("Rec_Date"), "MM/dd/yyyy HH:mm:ss" ) ) ;
                                    }
                                    else
                                    {
                                        inventoryLot.setDateIn(StringUtils.formatDateToString(new Date()) );
                                    }
                                }
                            }

                            //SORT
                            if ( result.getString("Pro_Area") != null && "TEST".equalsIgnoreCase( result.getString("Pro_Area") ) )
                            {
                                log( "In Sort " + lotNumber ) ;

                                fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fabLotNumber );
                                if (fabSortLot == null)
                                {
                                    fabSortLot = new FabSortLot( VENDOR_NAME_UTAC, WIP_TYPE_SORT );
                                    m_sortDataMap.put( partNumber + fabLotNumber, fabSortLot);
                                }

                                int quantity = 0;
                                if (fabSortLot.getQtyIn() != null)
                                {
                                    try
                                    {
                                        quantity = Integer.parseInt(fabSortLot.getQtyIn());
                                    }
                                    catch (NumberFormatException ignore) {
                                    }
                                    quantity += result.getInt("Cur_Qty");
                                }
                                else
                                {
                                    quantity = result.getInt("Cur_Qty");
                                }
                                if ( quantity > 0 )
                                {
                                    fabSortLot.setLotId( fabLotNumber );
                                    fabSortLot.setCustProduct( partNumber );
                                    fabSortLot.setStage( result.getString("Cur_Stage"));
                                    fabSortLot.setPoNumber( result.getString("PO_NUM") );
                                    fabSortLot.setFabLotNumber( fabLotNumber );
                                    fabSortLot.setQtyIn( quantity +"");
                                    fabSortLot.setLotCategory ( LOT_CATEGORY_TYPE_PROD );

                                    if ( result.getDate("LotStart_Date") != null  )
                                    {
                                        fabSortLot.setDateIn( StringUtils.formatDateToString( result.getDate("LotStart_Date"),"MM/dd/yyyy HH:mm:ss") ) ;
                                    }
                                    else if ( result.getDate("Rec_Date") != null  )
                                    {
                                        fabSortLot.setDateIn( StringUtils.formatDateToString(result.getDate("Rec_Date"), "MM/dd/yyyy HH:mm:ss" ) ) ;
                                    }
                                    else
                                    {
                                        fabSortLot.setDateIn(StringUtils.formatDateToString(new Date()) );
                                    }

                                    if ( result.getDate("Sch_Ship_Date") != null  )
                                    {
                                        fabSortLot.setForcastedDateOut( StringUtils.formatDateToString(result.getDate("Sch_Ship_Date"), "MM/dd/yyyy HH:mm:ss" ) );
                                    }
                                    fabSortLot.setLotStatus( lotStatus );
                                }
                            }
                        }
                    }//End of While
                }
                else
                {
                    log("sheetName = " + sheetName );
                    ((XLSParserImpl)parser).setHeaderRowIdx(2);
                    ((XLSParserImpl)parser).setDataStartRowIdx(3);

                    result = ((XLSParserImpl)parser).parse( sheetName );
                    //System.out.println("result.getHeadings() = " + result.getHeadings());
                    while ( result.next() )
                    {
                        partNumber = result.getString("Cust Device");
                        fabLotNumber = result.getString("Cust LotID");
                        lotNumber  = result.getString("Lot ID");

                        if( result.getString("Cur_State") != null && ( "WAIT".equalsIgnoreCase( result.getString("Cur_State") )
                                || "HELD".equalsIgnoreCase(result.getString("Cur_State") ) || result.getString("Cur_State").indexOf("HLD") != -1 ) )
                        {
                            lotStatus = LOT_STATUS_HOLD;
                        }
                        else
                        {
                            lotStatus = LOT_STATUS_RUNNING;
                        }

                        if ( lotNumber != null && lotNumber.trim().length() > 0 && partNumber != null && partNumber.trim().length() > 0)
                        {
                            if ( result.getString("Pro_Area") != null && "ASSEMBLY".equalsIgnoreCase( result.getString("Pro_Area") ) && "P200-DBANK".equalsIgnoreCase( result.getString("Cur_Stage") ) )
                            {
                                inventoryLot = (InventoryLot) m_dieBankDataMap.get( partNumber + fabLotNumber  );
                                if ( inventoryLot == null )
                                {
                                    inventoryLot = new InventoryLot(VENDOR_NAME_UTAC, LOCATION_TYPE_DIE_BANK );
                                    m_dieBankDataMap.put( partNumber + fabLotNumber , inventoryLot );
                                }
                                int quantity = 0;
                                if (inventoryLot.getQtyIn() != null)
                                {
                                    try
                                    {
                                        quantity = Integer.parseInt(inventoryLot.getQtyIn());
                                    }
                                    catch (NumberFormatException ignore) {
                                    }
                                    quantity += result.getInt("Cur_Qty");
                                }
                                else
                                {
                                    quantity = result.getInt("Cur_Qty");
                                }
                                String startDate=null;
                                String dateIn = null;
                                if(inventoryLot.getDateIn() != null )
                                {
                                    dateIn = inventoryLot.getDateIn();
                                }
                                if (result.getDate("LotStart_Date") != null)
                                {
                                    startDate = StringUtils.formatDateToString( result.getDate("LotStart_Date"),"MM/dd/yyyy HH:mm:ss");
                                }
                                //for getting MIn Date
                                if((startDate != null ) &&( dateIn != null ))
                                {
                                    Date tempDate1 = StringUtils.format( dateIn,"MM/dd/yyyy HH:mm:ss");
                                    Date tempDate2 = StringUtils.format(startDate,"MM/dd/yyyy HH:mm:ss");
                                    if ( tempDate1.compareTo( tempDate2 ) < 0 )
                                    {
                                        startDate = dateIn;
                                    }
                                }

                                if ( quantity > 0 )
                                {
                                    inventoryLot.setLotNumber( fabLotNumber );
                                    inventoryLot.setPartNumber( partNumber );
                                    inventoryLot.setStatus( lotStatus );
                                    inventoryLot.setFabLotNumber( fabLotNumber );
                                    inventoryLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                                    inventoryLot.setQtyIn( quantity +"" );
                                    inventoryLot.setDateIn( startDate );
                                    inventoryLot.setStage( result.getString("Cur_Stage") );
                                    inventoryLot.setCustomerPO( result.getString("PO_NUM") );
                                }
                            }
                            //ASSEMBLY
                            if( result.getString("Pro_Area") != null && ("ASSEMBLY".equalsIgnoreCase( result.getString("Pro_Area") ) || "FINISH".equalsIgnoreCase( result.getString("Pro_Area") ) ) &&  !"P200-DBANK".equalsIgnoreCase( result.getString("Cur_Stage") ) )
                            {
                                log( "In Assembly " + lotNumber ) ;

                                assemblyLot = (AssemblyLot) m_assyWipDataMap.get( partNumber + lotNumber + fabLotNumber );
                                if (assemblyLot == null)
                                {
                                    assemblyLot = new AssemblyLot( VENDOR_NAME_UTAC );
                                    m_assyWipDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                                }

                                int quantity = 0;
                                if (assemblyLot.getQtyIn() != null)
                                {
                                    try
                                    {
                                        quantity = Integer.parseInt(assemblyLot.getQtyIn());
                                    }
                                    catch (NumberFormatException ignore) {
                                    }
                                    quantity += result.getInt("Cur_Qty");
                                }
                                else
                                {
                                    quantity = result.getInt("Cur_Qty");
                                }
                                if ( quantity > 0 )
                                {
                                    assemblyLot.setDeviceName(partNumber);
                                    assemblyLot.setLotNumber(lotNumber);
                                    assemblyLot.setQtyIn(quantity + "");
                                    if ( result.getDate("LotStart_Date") != null  )
                                    {
                                        assemblyLot.setDateIn( StringUtils.formatDateToString( result.getDate("LotStart_Date"),"MM/dd/yyyy HH:mm:ss") ) ;
                                    }
                                    else if ( result.getDate("Rec_Date") != null  )
                                    {
                                        assemblyLot.setDateIn( StringUtils.formatDateToString( result.getDate("Rec_Date"),"MM/dd/yyyy HH:mm:ss") ) ;
                                    }

                                    assemblyLot.setFabLotNumber(fabLotNumber);
                                    assemblyLot.setStatus(lotStatus );

                                    assemblyLot.setCategory(LOT_CATEGORY_TYPE_PROD);
                                    assemblyLot.setStage( result.getString("Cur_Stage") );
                                    assemblyLot.setDateCode( result.getString("Date_Code") );
                                    assemblyLot.setPONumber(result.getString("PO_NUM") );
                                    if ( result.getDate("Sch_Ship_Date") != null  )
                                    {
                                         assemblyLot.setExpectedDateOut(StringUtils.formatDateToString(result.getDate("Sch_Ship_Date"), "MM/dd/yyyy HH:mm:ss" ));
                                    }
                                }
                            }
                            //TEST
                            if( result.getString("Pro_Area") != null &&  ("TEST".equalsIgnoreCase(result.getString("Pro_Area") ) || "EOL".equalsIgnoreCase(result.getString("Pro_Area") ) ) )
                            {

                                log( "In Test " + lotNumber );

                                testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber);

                                if (testLot == null)
                                {
                                    testLot = new TestLot(VENDOR_NAME_UTAC );
                                    m_ftDataMap.put( partNumber + lotNumber + fabLotNumber, testLot);
                                }

                                int quantity = 0;
                                if (testLot.getQtyIn() != null)
                                {
                                    try
                                    {
                                        quantity = Integer.parseInt(testLot.getQtyIn());
                                    }
                                    catch (NumberFormatException ignore) {
                                    }
                                    quantity += result.getInt("Cur_Qty");
                                }
                                else
                                {
                                    quantity = result.getInt("Cur_Qty");
                                }
                                if ( quantity > 0 )
                                {
                                    testLot.setPartNumber(partNumber);
                                    testLot.setLotNumber(lotNumber);
                                    testLot.setQtyIn(quantity + "");
                                    if ( result.getDate("LotStart_Date") != null  )
                                    {
                                        testLot.setDateIn( StringUtils.formatDateToString( result.getDate("LotStart_Date"),"MM/dd/yyyy HH:mm:ss") ) ;
                                    }
                                    else if ( result.getDate("Rec_Date") != null  )
                                    {
                                        testLot.setDateIn( StringUtils.formatDateToString( result.getDate("Rec_Date"),"MM/dd/yyyy HH:mm:ss") ) ;
                                    }

                                    if ( result.getDate("Sch_Ship_Date") != null  )
                                    {
                                         testLot.setExpecetedDate(StringUtils.formatDateToString(result.getDate("Sch_Ship_Date"), "MM/dd/yyyy HH:mm:ss" ));
                                    }
                                    testLot.setFabLotNumber(fabLotNumber);
                                    testLot.setStatus(lotStatus);
                                    testLot.setPsType(LOCATION_TYPE_FT);
                                    testLot.setCategory(LOT_CATEGORY_TYPE_PROD );
                                    testLot.setStage( result.getString("Cur_Stage") );
                                    testLot.setDateCode( result.getString("Date_Code") );
                                    testLot.setPoNumber( result.getString("PO_NUM") );
                                }
                            }
                            // FG
                            if( result.getString("Pro_Area") != null && "PTC".equalsIgnoreCase(result.getString("Pro_Area") ) )
                            {
                                log( "In FG " + lotNumber );

                                inventoryLot = (InventoryLot) m_fgDataMap.get( partNumber + lotNumber + fabLotNumber);

                                if (inventoryLot == null)
                                {
                                    inventoryLot = new InventoryLot(VENDOR_NAME_UTAC, LOCATION_TYPE_FG );
                                    m_fgDataMap.put( partNumber + lotNumber + fabLotNumber, inventoryLot);
                                }
                                int quantity = 0;
                                if (inventoryLot.getQtyIn() != null)
                                {
                                    try
                                    {
                                        quantity = Integer.parseInt(inventoryLot.getQtyIn());
                                    }
                                    catch (NumberFormatException ignore) {
                                    }
                                    quantity += result.getInt("Cur_Qty");
                                }
                                else
                                {
                                    quantity = result.getInt("Cur_Qty");
                                }

                                if ( quantity > 0 )
                                {
                                    inventoryLot.setLotNumber( lotNumber );
                                    inventoryLot.setPartNumber( partNumber );
                                    inventoryLot.setStatus( lotStatus );
                                    inventoryLot.setFabLotNumber( fabLotNumber );
                                    inventoryLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                                    inventoryLot.setStage( result.getString("Cur_Stage") );
                                    inventoryLot.setCustomerPO( result.getString("PO_NUM") );
                                    if ( result.getDate("LotStart_Date") != null  )
                                    {
                                        inventoryLot.setDateIn( StringUtils.formatDateToString( result.getDate("LotStart_Date"),"MM/dd/yyyy HH:mm:ss") ) ;
                                    }
                                    else if ( result.getDate("Rec_Date") != null  )
                                    {
                                        inventoryLot.setDateIn( StringUtils.formatDateToString( result.getDate("Rec_Date"),"MM/dd/yyyy HH:mm:ss") ) ;
                                    }
                                    inventoryLot.setQtyIn( quantity +"");

                                    inventoryLot.setDateCode( result.getString("Date_Code") );
                                }
                           }
                       }
                    }//End Of While
                }
            }//End of For llop
        }
        catch (Exception ex)
        {
            System.out.println("ERROR!! ERROR!! While parsing the UTAC Assy & Test Wip Data Details........" + ex );
            ex.printStackTrace();
            throw ex;
        }
    }

    private static void log( String message )
    {
        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            synchronized ( m_parserLogger )
            {
                m_parserLogger.logDebug( message + LINE_SEPARATOR );
            }
        }
        else
        {
            System.out.println( message );
        }
    }
}
